package com.siro.pm.ui.main;

import java.util.List;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.DeveloperProvider;
import com.siro.pm.ui.detail.AddDeveloperPage;
import com.siro.pm.ui.detail.DeveloperDetailPage;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since DevelopersPage.java v1.0
 */
public class DevelopersPage extends VerticalLayout {
	private static final long serialVersionUID = -6963599324304409783L;

	DevelopersPage self;
	private Project project;
	// private List<Developer> developers;

	private Button button_AddDeveloper;
	private Button button_DeleteDeveloper;
	public Table developersTable;

	public DevelopersPage() {
		self = this;
	}

	public DevelopersPage(Project project, List<Developer> developers) {
		this.project = project;
		// this.developers = developers;
	}

	public void createContents() {
		setSizeFull();
		setSpacing(true);
		setMargin(true, true, true, true);

		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		// Add Bug
		button_AddDeveloper = new Button(I18nManager.getMessage("button.add"));
		button_AddDeveloper.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				if (project != null) {
					AddDeveloperPage dvpDetailPage = new AddDeveloperPage();
					dvpDetailPage.setDevelopersPage(self);
					dvpDetailPage.setOwnerProject(project);
					dvpDetailPage.createContents();
					Window dvpDetailWindow = new Window(I18nManager
							.getMessage("developer.add"));
					dvpDetailWindow.setModal(true);
					dvpDetailWindow.setWidth("800px");
					dvpDetailWindow.setHeight("80%");
					dvpDetailPage.setWindow(dvpDetailWindow);
					dvpDetailWindow.addComponent(dvpDetailPage);
					getWindow().addWindow(dvpDetailWindow);
				} else {
					DeveloperDetailPage developerDetailPage = new DeveloperDetailPage();
					developerDetailPage.setDevelopersPage(self);
					developerDetailPage.createContents();
					Window dvpDetailWindow = new Window(I18nManager
							.getMessage("developer.add"));
					dvpDetailWindow.setModal(true);
					dvpDetailWindow.setWidth("800px");
					dvpDetailWindow.setHeight("80%");
					developerDetailPage.setWindow(dvpDetailWindow);
					dvpDetailWindow.addComponent(developerDetailPage);
					getWindow().addWindow(dvpDetailWindow);
				}
			}
		});
		buttons.addComponent(button_AddDeveloper);
		buttons.setComponentAlignment(button_AddDeveloper, Alignment.TOP_RIGHT);

		// Delete Developer
		button_DeleteDeveloper = new Button(
				I18nManager.getMessage("button.delete"));
		button_DeleteDeveloper.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				Object object = developersTable.getValue();
				if (object != null) {
					if (project != null) {
						Developer developer = (Developer) object;
						Project.deleteDeveloper(project, developer);
						developersTable.removeItem(developer);
					} else {
						Developer developer = (Developer) object;
						Developer.deleteDeveloper(developer);
						developersTable.removeItem(developer);
					}
				}
			}
		});
		buttons.addComponent(button_DeleteDeveloper);
		buttons.setComponentAlignment(button_DeleteDeveloper,
				Alignment.TOP_RIGHT);

		if (project != null
				&& !Developer.isProjectManager(project.getManager())) {
			button_AddDeveloper.setEnabled(false);
			button_DeleteDeveloper.setEnabled(false);
		}

		addComponent(buttons);
		setComponentAlignment(buttons, Alignment.TOP_RIGHT);

		// Developers Table
		developersTable = new Table();
		developersTable.setWidth("100%");
		developersTable.setHeight("100%");
		developersTable.setSelectable(true);
		// developersTable.setColumnWidth(
		// DeveloperProvider.DEVELOPER_PROPERTY_NAME, 100);
		// developersTable.setColumnWidth(
		// DeveloperProvider.DEVELOPER_PROPERTY_MAILADDR, 100);
		developersTable.setContainerDataSource(DeveloperProvider
				.getDevelopersData(project == null ? null : project
						.getDevelopers()));
		developersTable.setColumnReorderingAllowed(true);
		developersTable.setColumnCollapsingAllowed(true);

		addComponent(developersTable);
		setExpandRatio(developersTable, 1);

	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	// public List<Developer> getDevelopers() {
	// return developers;
	// }
	//
	// public void setDevelopers(List<Developer> developers) {
	// this.developers = developers;
	// }
}
